package com.wechet.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.wechet.common.ProductStatus;
import com.wechet.domain.ProductCategory;
import com.wechet.domain.ProductInfo;
import com.wechet.exception.SellException;
import com.wechet.form.ProductForm;
import com.wechet.serveice.ProductInfoService;
import com.wechet.serveice.impl.ProductCategoryServiceImpl;
import com.wechet.serveice.impl.ProductInfoServiceImpl;
import com.wechet.utils.keyUtils;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sun.security.util.KeyUtil;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by sc on 2017/9/27.
 */

@RequestMapping("/seller/product")
@Controller
public class SellerProductController {

    @Autowired
    private ProductInfoServiceImpl productInfoService;

    @Autowired
    private ProductCategoryServiceImpl productCategoryService;

    @GetMapping("/list")
    public ModelAndView listall(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "5") Integer size,
                                Map<String, Object> map) {
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<ProductInfo> productInfoPage = productInfoService.findAll(pageRequest);
        map.put("productInfoPage", productInfoPage);
        map.put("currentPage", page);
        return new ModelAndView("product/list", map);
    }

    @GetMapping("/off_sale")
    public ModelAndView offSale(@RequestParam("productId") String productId,
                                Map<String, Object> map) {

        ProductInfo productInfo = new ProductInfo();
        try {
            productInfo = productInfoService.offSale(productId);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", map);
    }

    @GetMapping("/on_sale")
    public ModelAndView onSale(@RequestParam("productId") String productId,
                               Map<String, Object> map) {

        ProductInfo productInfo = new ProductInfo();
        try {
            productInfo = productInfoService.onSale(productId);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", map);

    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId,
                              Map<String, Object> map) {

        if (!StringUtils.isEmpty(productId)) {
            ProductInfo productInfo = productInfoService.findOne(productId);
            map.put("productInfo", productInfo);
        }
        List<ProductCategory> categoryList = productCategoryService.findAll();
        map.put("categoryList", categoryList);
        return new ModelAndView("/product/index", map);
    }


    @PostMapping("save")
    //@CachePut(cacheNames = "product",key = "123")
    @CacheEvict(cacheNames = "product", key = "123")
    public ModelAndView save(@Valid ProductForm form, BindingResult result, Map<String, Object> map) {
        if (result.hasErrors()) {
            map.put("msg", result.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);
        }
        ProductInfo productInfo = new ProductInfo();
        try {
            if (StringUtils.isEmpty(form.getProductId())) {
                BeanUtils.copyProperties(form, productInfo);
                productInfo.setProductStatus(ProductStatus.UP.getCode());
                productInfo.setProductId(keyUtils.genUniqueKey());
                productInfoService.save(productInfo);
            } else {
                productInfo = productInfoService.findOne(form.getProductId());
                BeanUtils.copyProperties(form, productInfo);
                productInfoService.save(productInfo);
            }
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", map);
    }


}
